package com.apple.framework.jserialcomm;

import com.fazecast.jSerialComm.SerialPort;
import com.fazecast.jSerialComm.SerialPortEvent;
import com.fazecast.jSerialComm.SerialPortPacketListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 串口事件监听器
 */
@Component
public class PacketListener implements SerialPortPacketListener {
    @Value("${jSerialComm.packetListener.packetSize:14}")
    private int packetSize;

    @Override
    public int getListeningEvents() { return SerialPort.LISTENING_EVENT_DATA_RECEIVED; }

    @Override
    public int getPacketSize() { return packetSize; }

    @Override
    public void serialEvent(SerialPortEvent event)
    {
        byte[] newData = event.getReceivedData();
        String message = new String(newData, 0, newData.length);
        System.out.println("message=" + message);
    }
}
